<?php


namespace app\agent\controller;

class AdminBase extends \app\common\Controller
{
	public $admin_info = [];
	public $page_no = 1;
	public $page_size = 10;
	public $page_size_max = 1.0E+18;
	public function __construct()
	{
		parent::__construct();
		if (input("page") != "") {
			$this->page_no = input("page");
		}
		if (input("limit") != "") {
			$this->page_size = input("limit");
		}
		if ($this->page_size == -1) {
			$this->page_size = $this->page_size_max;
		}
		header("content-type:application/json");
		$admin_token = \think\facade\Request::header("agent-token");
		$admin_id = \think\facade\Request::header("agent-id");
		if (!$admin_id || !$admin_token) {
			$this->response(\app\agent\model\Code::loginFailure, "请先登录");
		}
		$admin_info = \think\Db::name("app_agent")->where(["id" => $admin_id])->find();
		if (empty($admin_info) || $admin_info["token"] != $admin_token) {
			$this->response(\app\agent\model\Code::loginFailure, "登录失效,请重新登录");
		}
		$token_info = json_decode(hex2bin($admin_token), true);
		if (!is_array($token_info) || !isset($token_info["ip"]) || $token_info["ip"] != \request()->ip()) {
			$this->response(\app\agent\model\Code::loginFailure, "登录IP发生变化,请重新登录");
		}
		if (!is_array($token_info) || !isset($token_info["startTime"]) || time() - $token_info["startTime"] > 86400) {
			$this->response(\app\agent\model\Code::loginFailure, "登录状态过期,请重新登录");
		}
		$controller_action = parseUnderline(request()->controller()) . "/" . request()->action();
		$this->admin_info = $admin_info;
	}
	public function response($code, $msg, $data = [], ...$other)
	{
		if ($code != \app\agent\model\Code::success) {
			\think\Db::rollback();
		}
		$response = ["code" => $code, "msg" => $msg, "data" => $data];
		foreach ($other as $array) {
			$response[$array[0]] = $array[1];
		}
		echo json_encode($response, JSON_UNESCAPED_UNICODE);
		exit;
	}
}